package m202405.d17;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/17 15:30
 */
public class Q75 {

    public static void main(String[] args) {
        Q75 q75 = new Q75();
        q75.sortColors(new int[]{1, 2, 1});
    }

    public void sortColors(int[] nums) {
        if (nums.length <= 1) {
            return;
        }
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                swap(nums, i, j);
                j++;
            }
        }
        for (int i = j; i < nums.length; i++) {
            if (nums[i] == 1) {
                swap(nums, i, j);
                j++;
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
    }
}
